// Note: this code requires installation of user-written packages coefplot, estout, and reghdfe

cd ""

clear all
use "job_conjoint_anonymized.dta"

forvalues x=1/3 {
	gen Scenario`x'selected1 = (Scenario`x'Choice==1) if Scenario`x'Choice != .
	gen Scenario`x'selected2 = (Scenario`x'Choice==2) if Scenario`x'Choice != .
}

egen cluster_F=group(F_*) //creating second cluster


rename F_???_1 pay_???
rename F_???_2 pfp_???
rename F_???_3 eval_???
rename F_???_4 involv_???
rename F_???_5 income_???
rename F_???_6 demog_???
rename F_???_7 overtime_???
rename F_???_8 task_???


gen white = (Race_1==1 & Race_3!=1)
la def racebinary 1 "White" 0 "Minority"
la val white racebinary


//// "Democratic" and "Republican" are dummies created seperately before. 1 indicates observation is democratic/republican, otherwise 0.//Party
gen Democratic = (Party==2)
la def partybinary 1 "Democratic" 0 "Nondemocratic"
la val Democratic partybinary
gen Republican = (Party==1)
la def partybinary2 1 "Republican" 0 "Nonrepublican"
la val Republican partybinary2
la var Republican "Republican"


reshape long pay_1_ pfp_1_ eval_1_ involv_1_ income_1_ demog_1_ overtime_1_ task_1_ Scenario1selected Scenario1Job12_ pay_2_ pfp_2_ eval_2_ involv_2_ income_2_ demog_2_ overtime_2_ task_2_ Scenario2selected Scenario2Job12_ pay_3_ pfp_3_ eval_3_ involv_3_ income_3_ demog_3_ overtime_3_ task_3_ Scenario3selected Scenario3Job12_ , i(ResponseId) j(job)
		 		 
rename *_ *
rename Scenario?selected selected_?
rename Scenario?Job12 Job12_?
rename Scenario?Choice Choice_?

reshape long pay_ pfp_ eval_ involv_ income_ demog_ overtime_ task_ selected_  Job12_ Choice_ , i(ResponseId job) j(scenario)

rename *_ *

foreach var of varlist pay pfp eval involv income demog overtime task {
	tab `var', gen(`var'_v)
}

//psm as a dummy (Table B2)
factor PSM_1-PSM_5, pcf
predict psm
egen meanpsm=mean(psm)
gen psmdummy=1 if psm>meanpsm
replace psmdummy=0 if psmdummy==.
la var psmdummy "High PSM"


gen pay_v31=.
replace pay_v31=1 if pay_v3==0
replace pay_v31=0 if pay_v3==1
drop pay_v3
rename pay_v31 pay_v3
la var pay_v3 "Total pay"
la def Totalpaybinary 1 "Total pay binary: About average and Slightly above average" 0 "Otherwise1"
la val pay_v3 Totalpaybinary

gen pfp_v11=.
replace pfp_v11=1 if pfp_v1==0
replace pfp_v11=0 if pfp_v1==1
drop pfp_v1
rename pfp_v11 pfp_v1
la var pfp_v1 "Performance bonuses"
la def Performancebonusesbinary 1 "Performance bonuses binary: Yes" 0 "Otherwise2"
la val pfp_v1 Performancebonusesbinary

gen eval_v31=.
replace eval_v31=1 if eval_v3==0
replace eval_v31=0 if eval_v3==1
drop eval_v3
rename eval_v31 eval_v3
la var eval_v3 "Goal based evaluation"
la def Goalbasedevaluationbinary 1 "Goal based evaluation: Yes" 0 "Otherwise3"
la val eval_v3 Goalbasedevaluationbinary

gen involv_v31=.
replace involv_v31=1 if involv_v3==0
replace involv_v31=0 if involv_v3==1
drop involv_v3
rename involv_v31 involv_v3
la var involv_v3 "Current community involvement"
la def involvementbinary 1 "Current community involvement binary: Moderate and Frequent participation" 0 "Otherwise4"
la val involv_v3 involvementbinary

gen income_v31=.
replace income_v31=1 if income_v3==0
replace income_v31=0 if income_v3==1
drop income_v3
rename income_v31 income_v3
la var income_v3 "Community income"
la def Communityincomebinary 1 "Community income binary: Average and High income" 0 "Otherwise5"
la val income_v3 Communityincomebinary

gen demog_v41=.
replace demog_v41=1 if demog_v4==0
replace demog_v41=0 if demog_v4==1
drop demog_v4
rename demog_v41 demog_v4
la var demog_v4 "Community demographics"
la def Communitydemographicsbinary 1 "Community demographics binary: Non-White" 0 "Otherwise6"
la val demog_v4 Communitydemographicsbinary

gen overtime_v21=.
replace overtime_v21=1 if overtime_v2==0
replace overtime_v21=0 if overtime_v2==1
drop overtime_v2
rename overtime_v21 overtime_v2
la var overtime_v2 "Overtime work"
la def Overtimeworkbinary 1 "Overtime work binary: Occasionally and Frequently required" 0 "Otherwise7"
la val overtime_v2 Overtimeworkbinary

gen task_v11=.
replace task_v11=1 if task_v1==0
replace task_v11=0 if task_v1==1
drop task_v1
rename task_v11 task_v1
la var task_v1 "Key job task"
la def Keyjobtaskbinary 1 "Key job task binary: Interaction with community and organizational members" 0 "Otherwise8"
la val task_v1 Keyjobtaskbinary


*****Interaction terms (1)*********
gen paypartyinteraction=pay_v3*Republican
la var paypartyinteraction "Total pay binary: About average and Slightly above average X Republican"

gen pfppartyinteraction=pfp_v1*Republican
la var pfppartyinteraction "Performance bonuses binary: Yes X Republican"

gen evalpartyinteraction=eval_v3*Republican
la var evalpartyinteraction "Goal based evaluation: Yes X Republican"

gen involvpartyinteraction=involv_v3*Republican
la var involvpartyinteraction "Current community involvement binary: Moderate and Frequent participation X Republican"

gen incomepartyinteraction=income_v3*Republican
la var incomepartyinteraction "Community income binary: Average and High income X Republican"

gen demogpartyinteraction=demog_v4*Republican
la var demogpartyinteraction "Community demographics binary: Non-White X Republican"

gen overtimepartyinteraction=overtime_v2*Republican
la var overtimepartyinteraction "Overtime work binary: Occasionally and Frequently required X Republican"

gen taskpartyinteraction=task_v1*Republican
la var taskpartyinteraction "Key job task binary: Interaction with community and organizational members X Republican"

********interation term (2)**********
gen paypsminteraction=pay_v3*psmdummy
la var paypsminteraction "Total pay binary: About average and Slightly above average X High PSM"

gen pfppsminteraction=pfp_v1*psmdummy
la var pfppsminteraction "Performance bonuses binary: Yes X High PSM"

gen evalpsminteraction=eval_v3*psmdummy
la var evalpsminteraction "Goal based evaluation: Yes X High PSM"

gen involvpsminteraction=involv_v3*psmdummy
la var involvpsminteraction "Current community involvement binary: Moderate and Frequent participation X High PSM"

gen incomepsminteraction=income_v3*psmdummy
la var incomepsminteraction "Community income binary: Average and High income X High PSM"

gen demogpsminteraction=demog_v4*psmdummy
la var demogpsminteraction "Community demographics binary: Non-White X High PSM"

gen overtimepsminteraction=overtime_v2*psmdummy
la var overtimepsminteraction "Overtime work binary: Occasionally and Frequently required X High PSM"

gen taskpsminteraction=task_v1*psmdummy
la var taskpsminteraction "Key job task binary: Interaction with community and organizational members X High PSM"






****************************Table A1******************
reghdfe selected i.Party pay_v3 pfp_v1 eval_v3 involv_v3 income_v3 demog_v4 overtime_v2 task_v1 , noabsorb vce(cluster cluster_F ResponseId)
est store party3_1, title("Job selection")

reghdfe selected pay_v3##i.Party pfp_v1##i.Party eval_v3##i.Party involv_v3##i.Party income_v3##i.Party demog_v4##i.Party overtime_v2##i.Party task_v1##i.Party, noabsorb vce(cluster cluster_F ResponseId)
est store party3, title("Job selection")

reghdfe selected pay_v3 pfp_v1 eval_v3 involv_v3 income_v3 demog_v4 overtime_v2 task_v1 psmdummy paypsminteraction pfppsminteraction evalpsminteraction involvpsminteraction incomepsminteraction demogpsminteraction overtimepsminteraction taskpsminteraction, noabsorb vce(cluster cluster_F ResponseId)
est store psm2, title("Job selection")
 
est table party3_1 party3 psm2, b(%6.4f) star(0.1 0.05 0.01)
esttab party3_1 party3 psm2, ///
title(Table 1 Effects comparison among partisanship, PSM, and self-efficacy) /// 
mlabels(,titles) cells(b(fmt(4) star) se(par fmt(4))) ///
legend starlevels(* 0.05 ** 0.01) label ///
stats(F r2 N, fmt(3 3 0) label("F-statistic" "R-sqr" N)) ///
drop(0.pay_v3 0.pfp_v1 0.eval_v3 0.involv_v3 0.income_v3 0.demog_v4 0.overtime_v2 0.task_v1) ///
addnotes("Robust standard errors in parentheses.") ///
, using "table.rtf", replace  





**********************Figure 1********************
//MM graph//moderated by party
reghdfe selected pay_v3##i.Party pfp_v1##i.Party eval_v3##i.Party involv_v3##i.Party income_v3##i.Party demog_v4##i.Party overtime_v2##i.Party task_v1##i.Party, noabsorb vce(cluster cluster_F ResponseId)
estimates store model
margins, at(Party=1 pay_v3=(0 1)) at(Party=1 pfp_v1=(0 1)) at(Party=1 eval_v3=(0 1)) at(Party=1 involv_v3=(0 1)) at(Party=1 income_v3=(0 1)) at(Party=1 demog_v4=(0 1)) at(Party=1 overtime_v2=(0 1)) at(Party=1 task_v1=(0 1)) post
estimates store marginsparty1, title("Republican")

estimates restore model
margins, at(Party=2 pay_v3=(0 1)) at(Party=2 pfp_v1=(0 1)) at(Party=2 eval_v3=(0 1)) at(Party=2 involv_v3=(0 1)) at(Party=2 income_v3=(0 1)) at(Party=2 demog_v4=(0 1)) at(Party=2 overtime_v2=(0 1)) at(Party=2 task_v1=(0 1)) post
estimates store marginsparty2, title("Democratic")

estimates restore model
margins, at(Party=3 pay_v3=(0 1)) at(Party=3 pfp_v1=(0 1)) at(Party=3 eval_v3=(0 1)) at(Party=3 involv_v3=(0 1)) at(Party=3 income_v3=(0 1)) at(Party=3 demog_v4=(0 1)) at(Party=3 overtime_v2=(0 1)) at(Party=3 task_v1=(0 1)) post
estimates store marginsparty3, title("Other")
 
	
coefplot (marginsparty1, label(Republican)) (marginsparty3, label(Independent)) (marginsparty2, label(Democratic)), ///
	xline(0.5) ciopts(recast(rcap) lcolor()) msize(*0.7) ///
	coeflabels(1._at = "Slightly below average" 2._at = "About average and Slightly above average" 3._at = "No (fixed salary)" 4._at = "Yes (bonuses)" 5._at = "No (supervisor based)" 6._at = "Yes (goal based)" 7._at = "Rare participation" 8._at = "Moderate and Frequent participation" 9._at = "Low income" 10._at = "Average and high income" 11._at = "Mostly white" 12._at = "Non-white" 13._at = "Never" 14._at = "Occasionally and Frequently" 15._at = "Analysis identifying community needs" 16._at = "Interaction with community and organizational members") ///
	headings(1._at = "{bf:Total Pay:}" 3._at = "{bf:Performance Bonuses:}" 5._at = "{bf:Goal Based Evaluation:}" 7._at = "{bf:Current Community Involvement:}" 9._at = "{bf:Community Income:}" 11._at = "{bf:Community Demographics:}" 13._at = "{bf:Overtime Work:}" 15._at = "{bf:Key Job Task:}") ///
	scheme(s1mono) legend(rows(1)) ///
	scale(*.5) ///
	xsize(9.5) ysize(8) xtitle(Marginal Mean)

	
//Table C1	
tab psmdummy Party, col chi2
